package cn.daomeng.firebird.core.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.hutool.core.util.XmlUtil;

/**
 * XML工具类接口
 * @author yong_pliang
 * @since 14:53 2014/09/04
 */
public class XmlUtils extends XmlUtil{

	private static Pattern p1 = Pattern.compile("<[^>]+>");
	private static Pattern p2 = Pattern.compile("(\\w+)\\s*=\\s*\"([^\"]+)\"");

	public static List<String> getElementByRegex(String xml, String regex) {
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(xml);
		ArrayList<String> al = new ArrayList<String>();
		while (m.find()) {
			al.add(m.group());
		}
		return al;
	}

	public static String getAttribute(String elementString, String attributeName) {
		elementString = elementString.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
		HashMap hm = new HashMap<String, String>(10);
		Matcher m = p1.matcher(elementString);
		String tmp = m.find() ? m.group() : "";
		m = p2.matcher(tmp);
		while (m.find()) {
			if (m.group(1).trim().equals(attributeName)){
				return m.group(2).trim();
			}
		}
		return "";
	}

}
